package com.sptpc.exam;

import com.sptpc.exam.model.User;
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/saveedit.do")
public class SaveEditServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决中文传值乱码
        request.setCharacterEncoding("utf-8");

        int uid = Integer.parseInt(request.getParameter("uid"));
        String tname = request.getParameter("tname");
        String major = request.getParameter("major");
        List<User> users = (List<User>)request.getSession().getAttribute("users");
        User user = null;
        for(User u : users){
            if(u.getUid() == uid) {
                user = u;
                user.setMajor(major);
                user.setTrueName(tname);
                break;
            }
        }

        //修改完成后跳转回userlist.jsp页面显示所有账号
        request.getRequestDispatcher("userlist.jsp").forward(request,response);

    }
}
